package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllPictures", query = "select myPicture from Picture myPicture"),
		@NamedQuery(name = "findPictureByName", query = "select myPicture from Picture myPicture where myPicture.name = ?1"),
		@NamedQuery(name = "findPictureByNameContaining", query = "select myPicture from Picture myPicture where myPicture.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findPictureByPath", query = "select myPicture from Picture myPicture where myPicture.path = ?1"),
		@NamedQuery(name = "findPictureByPathContaining", query = "select myPicture from Picture myPicture where myPicture.path like concat('%',?1,'%')"),
		@NamedQuery(name = "findPictureByDescription", query = "select myPicture from Picture myPicture where myPicture.description = ?1"),
		@NamedQuery(name = "findPictureByDescriptionContaining", query = "select myPicture from Picture myPicture where myPicture.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllPicturesByEvent", query = "select myPicture from Picture myPicture where myPicture.event.id = ?1"),
		@NamedQuery(name = "findAllPicturesByFood", query = "select myPicture from Picture myPicture where myPicture.food.id = ?1"),
		@NamedQuery(name = "findPictureById", query = "select myPicture from Picture myPicture where myPicture.id = ?1"),
		@NamedQuery(name = "findPictureByPrimaryKey", query = "select myPicture from Picture myPicture where myPicture.id = ?1 "),
		@NamedQuery(name = "deletePicturesByIds", query = "delete Picture where id in (?1)"),
		@NamedQuery(name = "findPicturesByIds", query = "select myPicture from Picture myPicture where myPicture.id in (?1)"),
		@NamedQuery(name = "findPictureByPhyscialPrimaryKey", query = "select myPicture from Picture myPicture where  myPicture.name = ?1 and myPicture.path = ?2"),
		@NamedQuery(name = "countPicturesByName", query = "select count(myPicture)  from Picture myPicture where myPicture.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "picture")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Picture")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Picture implements Serializable, Comparable<Picture> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "path")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String path;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "event", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Event event;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "food", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Food food;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPath() {
		return this.path;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public Event getEvent() {
		return event;
	}

	public void setFood(Food food) {
		this.food = food;
	}

	public Food getFood() {
		return food;
	}

	public void copy(Picture that) {

		setId(that.getId());

		setName(that.getName());
		setPath(that.getPath());
		setDescription(that.getDescription());

		setEvent(that.getEvent());

		setFood(that.getFood());

	}

	public Picture() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("path=[").append(path).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Picture))
			return false;
		Picture comparedPicture = (Picture) obj;
		if (!CheckPropertyEqual(this.name, comparedPicture.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.path, comparedPicture.path)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Picture o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
